package com.fesco.mis.fm.util.serializeutils;


import com.fesco.mis.fm.util.serializeutils.serializer.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * 对象序列化工具包
 *
 * @author winterlau
 */
public class ByteSerializationUtils {

    private final static Logger log = LoggerFactory.getLogger(ByteSerializationUtils.class);
    private static Serializer javaSerialize = new JavaSerializer();
    private static Serializer fstSerialize = new FSTSerializer();
    private static Serializer kryoSerialize = new KryoSerializer();
    private static Serializer kryoPoolSerialize = new KryoPoolSerializer();

    public static byte[] serialize(Object obj, SerializTypeEnum type) throws IOException {
        byte[] serializeResult;
        switch (type) {
            case Kryo:
                serializeResult = kryoSerialize.serialize(obj);
                break;
            case KryoPool:
                serializeResult = kryoPoolSerialize.serialize(obj);
                break;
            case FST:
                serializeResult = fstSerialize.serialize(obj);
                break;
            case JAVA:
                serializeResult = javaSerialize.serialize(obj);
                break;
            default:
                serializeResult = kryoSerialize.serialize(obj);
                break;
        }
        return serializeResult;
    }

    public static Object deserialize(byte[] bytes, SerializTypeEnum type) throws IOException {
        Object deSerializeResult;
        switch (type) {
            case Kryo:
                deSerializeResult = kryoSerialize.deserialize(bytes);
                break;
            case KryoPool:
                deSerializeResult = kryoPoolSerialize.deserialize(bytes);
                break;
            case FST:
                deSerializeResult = fstSerialize.deserialize(bytes);
                break;
            case JAVA:
                deSerializeResult = javaSerialize.deserialize(bytes);
                break;
            default:
                deSerializeResult = kryoSerialize.deserialize(bytes);
                break;
        }
        return deSerializeResult;
    }


}
